<%@ page import="java.util.ArrayList" %>
<%@ page import="domain.Product" %>
<%@ page import="dao.ProductDao" %>
<%@ page import="java.lang.String" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%--
  Created by IntelliJ IDEA.
  User: ThinkPad
  Date: 2021/12/23
  Time: 11:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>小汉堡主页</title>
</head>
<body>
<%@include file="head.jsp"%>
<div style="text-align: right;margin-right: 100px">
    <a href="${pageContext.request.contextPath}/cart.jsp">我的购物车</a>
    <%-- 实现搜索功能 --%>
    <form method="post" action="${pageContext.request.contextPath}/main.jsp">
        <label><input type="text" name="search"></label>
        <input type="submit" value="搜索">
    </form>
</div>
<%
    String search = request.getParameter("search");
    if (search !=null){
        request.getSession().setAttribute("pname", search);
        if (search.equals("")){
            request.getSession().removeAttribute("pname");
        }
    }
    String pname = (String) request.getSession().getAttribute("pname");
    if (pname!=null){
        out.println("<div style=\"margin-left: 120px;color: red;\">" +
                "<b>温馨提示</b>：您搜索的“"+pname+"”翻页后仍有效，若想清空您的搜索关键字，请直接点击”搜索“按钮</div>");
    }
%>
<br /><br />
<%-- 实现产品浏览功能 --%>
<div style="margin-left: 120px;color: indianred;"><b>产品浏览：</b></div>
<br />
<%
    String pageString = request.getParameter("page");
    ArrayList<Product> products = new ArrayList<>();
    ProductDao dao = new ProductDao();
    int pageIndex = 1;
    int totalPage = dao.getTotalPage(pname);
    try {
        // 页面的默认值是1
        pageIndex = pageString==null?1:Integer.parseInt(pageString);
        if (pageIndex<=0){
%>
<script>
    alert("已经是第一页了哦！");
    window.location="main.jsp?page=1";
</script>
<%
        }
        if (pageIndex>totalPage){
%>
<script>
    alert("已经是最后一页了哦！");
    window.location="main.jsp?page="+<%=totalPage%>;
</script>
<%
        }
        products = dao.browse(pageIndex,pname);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
%>
<%
    // 从session中获取购物车对象
    Map<Product,Integer> cart = (Map<Product, Integer>) session.getAttribute("cart");
    // 如果购物车为null，说明没有商品存储在购物车中，创建出购物车
    if (cart==null){
        cart = new HashMap<>();
        session.setAttribute("cart",cart);
    }
    String addString = request.getParameter("add");
    if (addString!=null){
        try {
            int add = Integer.parseInt(addString);
            Product p = dao.searchInMap(cart, add);
            if (p!=null){
                // 如果商品数量不为空，则商品数量+1，否则添加新的商品信息
                Integer count = cart.put(p,1);
                if (count!=null)
                    cart.put(p,count+1);
            }
            else {
                p = dao.search(add);
                cart.put(p, 1);
            }
            session.setAttribute("cart",cart);
            out.print("<script>alert(\"商品“"+p.getName()+"“加入购物车或增加数目成功！\");" +
                    "window.location=\"main.jsp?page="+pageIndex+"\";</script>");
        }catch (NumberFormatException e){
            e.printStackTrace();
        }
    }
%>
<%-- 利用JSTL标签输出产品的值 --%>
<c:forEach var="prod" items="<%=products%>">
    <table style="border: 3px solid sandybrown;margin-left: 15%;height: 220px;width: 70%">
        <tr>
            <td rowspan="3" style="width: 220px"><img src="${prod.img}" alt="" height="200px" width="200px"></td>
            <td colspan="3" style="font-size: large;margin-left: 20px"><b>${prod.name}</b></td>
        </tr>
        <tr>
            <td colspan="3" style="font-size: x-small;color:grey">${prod.description}</td>
        </tr>
        <tr>
            <td style="font-size: small;color:grey">库存${prod.count}件</td>
            <td style="text-align: right;color: crimson"><b>￥${String.format("%.2f", prod.price)}</b></td>
            <td style="width: 120px"><a href="${pageContext.request.contextPath}/main.jsp?page=<%=pageIndex%>&add=${prod.id}"><div style="text-align: right">加入购物车</div></a></td>
        </tr>
    </table>
    <br />
</c:forEach>
<br /><br />
<%-- 页码切换 --%>
<div style="text-align: center">
    <a href="${pageContext.request.contextPath}/main.jsp?page=<%=pageIndex-1%>">上一页</a>
    &nbsp;&nbsp;第<%=pageIndex%>页，共<%=totalPage%>页&nbsp;&nbsp;
    <a href="${pageContext.request.contextPath}/main.jsp?page=<%=pageIndex+1%>">下一页</a>
</div>
<%@include file="tail.jsp"%>
</body>
</html>
